package com.fanxl.flink.datastream.kafka;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;

import java.util.Map;
import java.util.Properties;

/**
 * @description:
 * @author: fanxiaole
 * @date: 2022/2/14 15:55
 */
@Slf4j
public class KafkaTest {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Properties props = new Properties();
        props.put("bootstrap.servers", "10.215.7.22:9092");
        props.put("group.id", "flinkEsper275");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("auto.offset.reset", "earliest");

        FlinkKafkaConsumer<String> consumer = new FlinkKafkaConsumer<>("log2json-test", new SimpleStringSchema(), props);
        DataStreamSource<String> dataStreamSource = env.addSource(consumer);
        DataStream<Map<String, Object>> dataStream = dataStreamSource.flatMap(new FlatMapFunction<String, Map<String, Object>>() {
            @Override
            public void flatMap(String value1, Collector<Map<String, Object>> out) throws Exception {
                log.info(value1);
                Map<String, Object> parse = (Map<String, Object>) JSON.parse(value1);
                out.collect(parse);
            }
        }).assignTimestampsAndWatermarks(new WordPeriodicWatermark<Map<String, Object>>());

        env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);


        dataStream.print();
        env.execute("flink-kafka-test");
    }
}
